import 'package:flutter/material.dart';

typedef VoidCallback = void Function();

enum CustomButtonType {
  stack,
  row, //从左到右
}

///自定义按键 ,可设置
// ignore: must_be_immutable
class CustomButton extends StatelessWidget {
  Widget? icon;
  Widget? text;
  final EdgeInsetsGeometry? iconPadding;
  final EdgeInsetsGeometry? textPadding;
  final VoidCallback? onPressed; //点击回调
  final CustomButtonType buttonType; //显示模式，从左到右，或者居中
  final bool touchLimit; //是否点击限制
  final int touchLimitSecond; //点击限制时长
  final bool? isInkWell; //是否水波纹组件
  final Color? highlightColor; //Ink的高亮颜色
  final BorderRadius? inkWellBorderRadius; //Ink组件使用，水波纹圆角

  CustomButton({
    Key? key,
    this.icon,
    this.text,
    this.onPressed,
    this.buttonType = CustomButtonType.row,
    this.iconPadding,
    this.textPadding,
    this.isInkWell,
    this.highlightColor,
    this.inkWellBorderRadius,
    this.touchLimit = true,
    this.touchLimitSecond = 1,
  }) : super(key: key);

  bool _isTap = false;

  void _onTapAction() {
    //1秒点一次
    if (_isTap == true && touchLimit == true) {
      return;
    }
    _isTap = true;
    if (onPressed != null) {
      onPressed!();
    }
    Future.delayed(Duration(seconds: touchLimitSecond)).then((_) {
      _isTap = false;
    });
  }

  Widget _stackWidget() {
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        if (icon != null)
          (iconPadding == null)
              ? icon!
              : Padding(
                  padding: iconPadding!,
                  child: icon!,
                ),
        if (text != null)
          (textPadding == null)
              ? text!
              : Padding(
                  padding: textPadding!,
                  child: text!,
                ),
      ],
    );
  }

  Widget _rowWidget() {
    return Row(
      children: <Widget>[
        if (icon != null)
          (iconPadding == null)
              ? icon!
              : Padding(
                  padding: iconPadding!,
                  child: icon!,
                ),
        if (text != null)
          (textPadding == null)
              ? text!
              : Padding(
                  padding: textPadding!,
                  child: text!,
                ),
      ],
    );
  }

  Widget _button() {
    if (buttonType == CustomButtonType.stack) {
      if (isInkWell == true) {
        return InkWell(
          borderRadius: inkWellBorderRadius ?? BorderRadius.circular(10),
          highlightColor: highlightColor,
          onTap: onPressed != null ? _onTapAction : null,
          child: _stackWidget(),
        );
      } else {
        return GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: onPressed != null ? _onTapAction : null,
          child: _stackWidget(),
        );
      }
    } else {
      if (isInkWell == true) {
        return InkWell(
          borderRadius: inkWellBorderRadius ?? BorderRadius.circular(10),
          highlightColor: highlightColor,
          onTap: onPressed != null ? _onTapAction : null,
          child: _rowWidget(),
        );
      } else {
        return GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: onPressed != null ? _onTapAction : null,
          child: _rowWidget(),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    icon ??= Container();
    text ??= Container();
    return _button();
  }
}
